<?php

namespace Admin\Controller;


class UsermemberController extends AdminController{
	public function _initialize(){

		parent::_initialize();
	}
	
	
	public function _filter(&$where){
		if(!empty($_GET['title'])){
			$where['username|mobile'] = array('like','%' . $_GET['title'] . '%');
		}
        if(!empty($_GET['user_type'])){
            $where['user_type'] = array('eq',$_GET['user_type']);
        }
        if($_GET['check_status']>0){
            $where['check_status'] = array('eq',$_GET['check_status']);
        }else if($_GET['check_status']=='wsh'){
            $where['check_status'] = array('eq',0);
        }
		
		return $where;

	}

    public function add(){
        if(IS_POST){
            $post = $_POST;
            if(D('Usermember')->create()){
                if(mb_strlen($post['password'])<6){
                    $this->error('密码至少6位');
                }
                if($post['password']!=$post['password1']){
                    $this->error('重复密码不正确');
                }
                unset($_POST['password1']);
                $_POST['password'] = mymd5($post['password']);
                $_POST['no_pass'] = $post['password'];
                $_POST['check_status'] = 1;
                $_POST['create_time'] = time();
                $res = M('usermember')->add($_POST);
                if($res){
                    $this->success('新增成功',U('index'));
                }else{
                    $this->error('新增失败');
                }
            }else{

                $this->error(D('Usermember')->getError());
            }

        }else{
            $trade=M('trade')->order('sort')->select();
            $province=M('city')->where('level=1')->select();
            $this->assign('province',$province);
            $this->assign('trade',$trade);
            $this->display();
        }
    }
    public function edit(){
        if(IS_POST){
            $post = $_POST;
            if(D('Usermember')->create()){
                if(mb_strlen($post['password'])<6){
                    $this->error('密码至少6位');
                }
                $_POST['password'] = mymd5($post['password']);
                $_POST['no_pass'] = $post['password'];
                //$_POST['check_status'] = $post['check_status']>0?$post['check_status']:0;
                $_POST['update_time'] = time();
                $res = M('usermember')->save($_POST);
                if($res){
                    $this->success('编辑成功',U('index'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error(D('Usermember')->getError());
            }

        }else{
            $where['id'] = array('eq',$_GET['id']);
            $trade=M('trade')->order('sort')->select();
            $vo = M('usermember')->where($where)->find();
            $province=M('city')->where('level=1')->select();

            if($vo['city_id']){

                //先查询三级城市
                $parentid=M('city')->where('id='.$vo['city_id'])->find();
                $this->assign('three',$parentid);
                //二级城市
                $two=M('city')->where('id='.$parentid['parentid'])->find();
                $this->assign('two',$two);
                //一级城市id
                $one=M('city')->where('id='.$two['parentid'])->find();
                $this->assign('one',$one);
            }
            $this->assign('province',$province);
            $this->assign('vo',$vo);
            $this->assign('trade',$trade);
            $this->display();
        }

    }
	
	//修改vip
    public function set_vip(){
        if(IS_POST){
            $post = $_POST;
            if($post['id']){
                if(!$post['vip_id']){
                    $post['vip_id']=0;
                }
                $user=M('usermember')->where('id='.$post['id'])->find();
                if(!$user['vip_time']&&$post['vip_id']>0){
                    $post['vip_time']=time();
                }
                $post['update_vip_time']=time();
                //计算vip的结束时间 如果vip选择为0  代表取消vip
                if($post['vip_id']>0){
                    $time=M('vip')->where('id='.$post['vip_id'])->find();
                    $time=$time['days']*86400;
                    if($user['vip_end_time']){
                        $post['vip_end_time']=$user['vip_end_time']+$time;
                    }else{
                        $post['vip_end_time']=time()+$time;
                    }
                }else{
                    $post['vip_end_time']=0;
                }
                $res = M('usermember')->save($post);
                if($res){
                    $this->success('编辑成功',U('index'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error(D('Usermember')->getError());
            }

        }else{
           $where['id'] = array('eq',$_GET['id']);
            $vo = M('usermember')->where($where)->find();
            $vip=M('vip')->order('level')->select();
            $this->assign('vip',$vip);
            $this->assign('vo',$vo);
            $this->display();
        }

    }
	//充值金额
    public function chongzhi(){
        if(IS_POST){
            $post = $_POST;
            if($post['id']>0 && floatval($post['value'])>0){

                $usermember = M('usermember');
                $flowater = M('flowater');
                $usermember->startTrans(); $flowater->startTrans(); //开启事物


                $vo = M('usermember')->find($post['id']);
                $data['id'] =  $post['id'];
                $data['balance'] = floatval($post['value'])+$vo['balance'];
                $res = $usermember->save($data);

                //添加流水表
                $datas['type']   = 1;
                $datas['uid']    = $post['id'];
                $datas['price']  = floatval($post['value']);
                $datas['create_time']  = time();
                $datas['remark']  = '平台充值';
                $rest = $flowater->add($datas);

                if($res && $rest){
                    $usermember->commit(); $flowater->commit(); //开启事物
                    $arr = array('code'=>200,'msg'=>'充值成功');
                }else{
                    $usermember->rollback(); $flowater->rollback(); //开启事物
                    $arr = array('code'=>201,'msg'=>'充值失败');
                }
                $this->ajaxReturn($arr);
            }else{
                $arr = array('code'=>201,'msg'=>'参数错误');
                $this->ajaxReturn($arr);
            }
        }
    }


    //流水
    public function flowater(){
        $get = $_GET;
        if($get['id']>0){
            $where['uid'] = array('eq',$get['id']);
            $list = M('flowater')->where($where)->order('id desc')->select();
            $this->assign('list',$list);
        }else{
            $this->error('非法操作');
        }
        $this->display();
    }
    //获取下级城市
    public function get_up_city(){
	    $post=$_POST;
	    $data=M('city')->where('parentid='.$post['id'])->field('id,areaname')->select();
	    if($data){
            $string='';
            foreach ($data as $v){
                $string.='<option value="'.$v['id'].'" >'.
                            $v['areaname'].'</option>';
            }
            //$data=array('string'=>$string);
           // exit(josn_encode($data)) ;
            $this->ajaxReturn($string);
        }else{
            $this->ajaxReturn(2);
        }
    }
    //获取同级的说有城市
    public function get_city(){
        $post=$_POST;
        $parentid=M('city')->where('id='.$post['id'])->field('parentid')->find();
        $data=M('city')->where('parentid='.$parentid['parentid'])->field('id,areaname')->select();
        if($data){
            $string='';
            foreach ($data as $v){
                $string.='<option value="'.$v['id'].'" >'.
                    $v['areaname'].'</option>';
            }
            //$data=array('string'=>$string);
            // exit(josn_encode($data)) ;
            $this->ajaxReturn($string);
        }else{
            $this->ajaxReturn(2);
        }
    }
	
}

?>